﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SILAS.BL;
using SILAS.Entidades;
using Microsoft.Practices.Unity;
using SILAS.UI.Models;
using SILAS.UI.ViewModels;


namespace SILAS.UI.Controllers
{
    public class SectorController : Controller
    {
        //
        // GET: /Sector/
        [Dependency]
        public ISectorBL SectorBL { get; set; }
        [Dependency]
        public IRutaBL RutaBL { get; set; }
        [Dependency]
        public IUbigeoBL UbigeoBL { get; set; }

        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult Index()
        {
            var listDistritos = UbigeoBL.ListDistritosByProvincia("01");
            var listCP = UbigeoBL.ListCentrosPobladosByDistrito("01");
            var listCaserios = UbigeoBL.ListCaseriosBycentroPoblado("01");

            ViewData["IdDistrito"] = new SelectList(listDistritos, "IdUbigeo", "Nombre");
            ViewData["IdCentroPoblado"] = new SelectList(listCP, "IdUbigeo", "Nombre");
            ViewData["IdCacerio"] = new SelectList(listCaserios, "IdUbigeo", "Nombre");
            
            return View();
        }

        //***** ACTION RESULT para cascada *****

        public ActionResult getServiceDistritoByIdProvJSON(string IdProvincia)
        {
            var query = from p in UbigeoBL.ListDistritosByProvincia(IdProvincia)
                        select new
                        {
                            value = p.IdUbigeo,
                            label = p.Nombre
                        };

            return Json(query.ToList(), JsonRequestBehavior.AllowGet); 
        }

        public ActionResult getServiceCPByIdDistrito(string IdDistrito)
        {
            var query = from p in UbigeoBL.ListCentrosPobladosByDistrito(IdDistrito)
                        select new
                        {
                            value = p.IdUbigeo,
                            label = p.Nombre
                        };

            return Json(query.ToList(), JsonRequestBehavior.AllowGet);
        }

        public ActionResult getServiceHijosByIdUbigeoRJSON(string IdUbigeoR)
        {
            var query = from p in UbigeoBL.ListHijosByIdUbigeoR(IdUbigeoR)
                        select new
                        {
                            value = p.IdUbigeo,
                            label = p.Nombre
                        };

            return Json(query.ToList(), JsonRequestBehavior.AllowGet);
        }

        //********* ENCONTRAR SECTOR SELECCIONANDO UN UBIGEO *****

        public ActionResult getSectorByUbigeo(string idubigeo)
        {

            var sector = SectorBL.getSectorbyUbigeo(idubigeo);

            if (sector != null)
            {
                var msg = new
                {
                    exito = true,
                    Sector = new
                    {
                        Id = sector.Id,
                        Ubigeo = sector.Ubigeo.Nombre
                    }
                };

                return Json(msg, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var msg = new { exito = false, message = "El Sector No Existe!!!" };
                return Json(msg, JsonRequestBehavior.AllowGet);
            }
            

            

       }


        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult ListarSectores()
        {
            var lista = SectorBL.ListarAllSectores();
            return View(lista);
        }

        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult MapaPartial()
        {
            return PartialView();
        }

        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult MapaJQuery()
        {
            return View();
        }


        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult MapaDistritoPartial()
        {
            return PartialView();
        }

        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult BusquedaSector()
        {
            var listDistritos = UbigeoBL.ListDistritosByProvincia("01");
            var listCP = UbigeoBL.ListCentrosPobladosByDistrito("01");
            var listCaserios = UbigeoBL.ListCaseriosBycentroPoblado("01");

            ViewData["IdDistrito"] = new SelectList(listDistritos, "IdUbigeo", "Nombre");
            ViewData["IdCentroPoblado"] = new SelectList(listCP, "IdUbigeo", "Nombre");
            ViewData["IdCacerio"] = new SelectList(listCaserios, "IdUbigeo", "Nombre");
                
            return View();
        }

        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult _ElegirSector()
        {

            var listDistritos = UbigeoBL.ListDistritosByProvincia("01");
            var listCP = UbigeoBL.ListCentrosPobladosByDistrito("01");
            var listCaserios = UbigeoBL.ListCaseriosBycentroPoblado("01");

            ViewData["IdDistrito"] = new SelectList(listDistritos, "IdUbigeo", "Nombre");
            ViewData["IdCentroPoblado"] = new SelectList(listCP, "IdUbigeo", "Nombre");
            ViewData["IdCacerio"] = new SelectList(listCaserios, "IdUbigeo", "Nombre");
                
            return PartialView();
        }

        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult ListarRutas(int id)
        {
            var lista = RutaBL.ListarRutasbySector(id);
            return PartialView(lista);
        }

        //
        // GET: /Sector/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Sector/Create
        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult Create()
        {
            var tipoVia = RutaBL.ListaTipoVia();
            var medioT = RutaBL.ListamedioTransporte();
            var cat = SectorBL.ListCategorias();

            //var Viewmodel = new SectorViewModel();
           
            //var sectores = SectorBL.ListarSectores();

            ViewData["IdTipoVia"] = new SelectList(tipoVia, "IdTipoVia", "Descripcion");
            ViewData["IdMedioTransp"] = new SelectList(medioT, "IdMedioTransp", "Descripcion");
            ViewData["IdCategoria"] = new SelectList(cat, "IdCategoria", "Descripcion");
            //ViewData["IdSector"] = new SelectList(sectores, "IdSector", "Descripcion");


            var listDistritos = UbigeoBL.ListDistritosByProvincia("01");
            var listCP = UbigeoBL.ListCentrosPobladosByDistrito("01");
            var listCaserios = UbigeoBL.ListCaseriosBycentroPoblado("01");

            ViewData["IdDistrito"] = new SelectList(listDistritos, "IdUbigeo", "Nombre");
            ViewData["IdCentroPoblado"] = new SelectList(listCP, "IdUbigeo", "Nombre");
            ViewData["IdCacerio"] = new SelectList(listCaserios, "IdUbigeo", "Nombre");


            return View();
        } 

        //
        // POST: /Sector/Create

        [HttpPost]
        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult Create(Sector sector, FormCollection collection)
        {
            try
            {

                // TODO: Add insert logic here
                SectorBL.agregarSector(sector);

                var result = new
                {
                    msg = "Sector Creado Correctamente",
                    exito = true
                };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch (SystemException ex)
            {
                var result = new
                {
                    msg = ex.Message,
                    exito = false
                };

                return Json(result, JsonRequestBehavior.AllowGet);
            }
        }
        
        //
        // GET: /Sector/Edit/5
        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult Edit(int id)
        {
            
            var sector = SectorBL.getSectorbyId(id);

            var tipoVia = RutaBL.ListaTipoVia();
            var medioT = RutaBL.ListamedioTransporte();
            var cat = SectorBL.ListCategorias();
            //var sectores = SectorBL.ListarSectores();

            ViewData["IdTipoVia"] = new SelectList(tipoVia, "IdTipoVia", "Descripcion");
            ViewData["IdMedioTransp"] = new SelectList(medioT, "IdMedioTransp", "Descripcion");
            ViewData["IdCategoria"] = new SelectList(cat, "IdCategoria", "Descripcion");
            //ViewData["IdSector"] = new SelectList(sectores, "IdSector", "Descripcion");

            return View(sector);
        }

        //
        // POST: /Sector/Edit/5

        [HttpPost]
        
        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
		public ActionResult Edit(Sector sector)            {
            try
            {
                // TODO: Add update logic here
                SectorBL.modificarSector(sector);

                var result = new
                {
                    msg = "La Información fue Guardada Correctamente",
                    exito = true
                };

                return Json(result, JsonRequestBehavior.AllowGet);

            }
            catch (SystemException ex)
            {
                var result = new
                {
                    msg = ex.Message,
                    exito = false
                };

                return Json(result, JsonRequestBehavior.AllowGet);
            }
        }

        //
        // GET: /Sector/Delete/5
        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Sector/Delete/5

        [HttpPost]
        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
